package cn.javaxc.mvcwork.framework.actions;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.controller.web.PagedListInfo;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.mvcwork.framework.database.*;
import cn.javaxc.mvcwork.modules.admin.*;
import cn.javaxc.mvcwork.modules.login.UserBean;
import cn.javaxc.mvcwork.controller.AppUtils;
import cn.javaxc.mvcwork.util.helper.RequestHelper;
import cn.javaxc.mvcwork.util.common.MSecurityUtils;
import java.io.*;
import java.lang.reflect.Method;
import java.sql.*;
import java.text.*;
import java.util.*;
import cn.javaxc.model.page.VUEPage;
import javax.servlet.http.*;
import org.springframework.web.servlet.ModelAndView;
import cn.javaxc.mvcwork.util.helper.FrameHelper;
import org.springframework.security.core.userdetails.UserDetails;
import com.ruoyi.common.core.domain.model.LoginUser;

public class TModuleAction extends TAction{
  public final static String SystemError="SystemError";
  public final static String PermissionError="PermissionError";
  public final static String fs = System.getProperty("file.separator");

	protected String doPermission()throws Exception{
		HttpServletRequest request=context.getRequest();
        String contentType = request.getContentType();
        String method = request.getMethod();
        if (System.getProperty("DEBUG")==null
				&&contentType != null
                && contentType.startsWith("multipart/form-data")
                && method.equalsIgnoreCase("POST")) {
			UserDetails userSession = MSecurityUtils.getUserEntity();
			if (userSession != null && userSession.getUsername()!=null) {
				return null;
			}
			return PermissionError;
		}
		return null;
	}

  protected PagedListInfo getPagedListInfo( String viewName) {
    return AppUtils.getPagedListInfo(context.getRequest(), viewName);
  }
  protected PagedListInfo getPagedListInfo( String viewName, boolean setParams) {
    return AppUtils.getPagedListInfo(context.getRequest(), viewName, true);
  }
  
  
  protected Connection _getConnection() throws SQLException {
    ConnectionElement ce = (ConnectionElement) context.getSession().getAttribute("ConnectionElement");
    return this._getConnection( ce);
  }


  
  protected Connection _getConnection( ConnectionElement ce) throws SQLException {
    ConnectionPool sqlDriver = (ConnectionPool) context.getServletContext().getAttribute( "ConnectionPool");
   	if (ce!=null){
		return sqlDriver.getConnection(ce);
	}
	return sqlDriver.getConnection();
  }

  protected void _freeConnection( Connection db) {
    if (db != null) {
      ConnectionPool sqlDriver = (ConnectionPool) context.getServletContext().getAttribute(
          "ConnectionPool");
      sqlDriver.free(db);
    }
    db = null;
  }



  

 
  protected SystemStatus getSystemStatus() {
    return AppUtils.getSystemStatus(context);
  }

  

  protected boolean hasPermission( String permission) {
    LoginUser thisUser = MSecurityUtils.getUserEntity();
    return thisUser.getPermissions().contains(permission);
  }
  
  protected void deletePagedListInfo( String viewName) {
    PagedListInfo tmpInfo = (PagedListInfo) context.getSession().getAttribute(
        viewName);
    if (tmpInfo != null) {
      context.getSession().removeAttribute(viewName);
    }
  }


  






  
  protected Object getReturn( Object resultString) {
	if(resultString==null||"-none-".equals(resultString))return null;

	return getReturn("ttpreturn",resultString);
  }
  protected Object getReturn(String returnString, Object resultString) {
	String ret = (String)RequestHelper.getRequestValue(context.getRequest(),returnString);
	if (ret!=null){
		return ret;
	}
	return doReturn(resultString);
  }

  protected Object doReturn(Object result) {
	if(result==null||"-none-".equals(result))return null;
	if (result instanceof ModelAndView){
		return result;
	}
	if (!(result instanceof String)){
		context.setAttribute("ajaxObject",result);
		return AJAXOBJECT;
	}
	String resultString=(String)result;
	if (AJAXOBJECT.equals(resultString)){
		return resultString;
//		try{
//			RequestHelper.writeJson(context.getResponse(),context.getAttribute("ajaxObject"));
//		}catch(IOException e){
//			throw new RuntimeException(e);
//		}
//		RequestUtil.ajaxSql(context.getRequest());
//		return null;
	}
	if (VUEPage.AJAXLIST.equals(resultString)){
		if(FrameHelper.getBaseSQL()!=null)FrameHelper.getBaseSQL().ajaxSql(context.getRequest());
	}
    boolean popup = "true".equals(RequestHelper.getRequestValue(context.getRequest(),"popup"));
    if (popup) {
      return (resultString += "PopupOK");
    }
    return (resultString += "OK");
  }

 protected String getPref( String param) {
	return AppUtils.getPref(context,param);
 }






  
  protected String getSystemPref( String configName, String param) {
    SystemStatus systemStatus = this.getSystemStatus();
    return systemStatus.getValue(configName, param);
  }


  
  protected boolean isPopup() {
    return ("true".equals(context.getParameter("popup")));
  }







  


  
  protected String getDbName() {
    return AppUtils.getDbName(context);
  }


  
  protected String getDbName(ConnectionElement ce) {
    if (ce != null) {
      return ce.getDbName();
    } else {
      return null;
    }
  }
  protected boolean processValidate() throws Exception {
	SystemStatus systemStatus = this.getSystemStatus();
    TFormBean bean=context.getFormBean();
	if (context.getActionError()!=null)return false;
	if (bean!=null){
	    if (bean.hasErrors()){
			 if (context.getActionError() == null) {
				context.setActionError("不能提交.请再阅读以下信息.");
			}
			return false;
		}
		if (bean.hasWarnings())return false;
	
	}
    return true;
  }
}
